{% extends "master.html" %}
{% block subtitle %} - 主面板 - 调拨管理 - 查找商品 - 申请调拨{% endblock %}

{% block extJS %}
<script language="JavaScript" type="text/javascript">
//<![CDATA[
    function ajaxSnd(obj,shopID){
        var obj = $(obj);
        obj.attr('disabled',true);
        
        var msg = [];
        clearMsg();
        $(".err").removeClass("err");        
        var atleastone = false;
        var qtyinput = true;
        var qtyok = true;
        $("input[name^='qty_'],input[name^='oldqty_']").each(function(){
            var t = $(this);
            var max = parseInt(t.attr('maxqty'));
            
            if(!t.val()){
                var qtyinput = false;
                t.addClass('err');
            }else if(!check_number(t.val())){
                msg.push('请正确填写数量,必须为数字！')   
            }else{
                var q = parseInt(t.val());
                if(q > max){
                    qtyok = false;
                    t.addClass('err');
                }
            }
            atleastone = true;          
        })
        
        if(!atleastone){
            msg.push('请填写至少一个商品！')
        }

        if(!qtyinput){
            msg.push('请填写调拨数量！');
        }
        
        if(!qtyok){
            msg.push('请正确填写数量，调拨数量必须少于现存数量！');
        }
        
        
        if(msg.length > 0){
            alertError(msg);
            obj.removeAttr('disabled');
            return false;
        }
        
                
        var row = $(obj.parents(".part")[0]);
        var params = {
            shopID : shopID,
            t : nowstr()
        };
        
        $(".pdtqty",row).each(function(){
            var t = $(this);
            params[t.attr('name')] = t.val()
        });
        
        
        $.getJSON("{{url_for('.view',action='ajaxAddDN')}}",
                    params,function(r){
                    
                    if(r.code != 0){
                        alert(r.msg);
                        obj.removeAttr('disabled');
                    }else{
                        alert(r.msg);
                        row.remove();
                        if($('.part').length < 1){
                            redirect("{{url_for('.view',action='searchpdt')}}");
                        }
                    }
                    
        })
    }
    
    function toCancel(){
        redirect_alert("{{MSG_LEAVE_WITHOUT_SAVING}}","{{url_for('bpTns.view',action='searchpdt')}}")
    } 
//]]>
</script>
{% endblock %}


{% block main %}
<div class="container">
    <div class="row">
        <ol class="breadcrumb">
          <li><a href="{{url_for('bpRoot.view')}}">主面板</a></li>
          <li><a href="{{url_for('bpTns.view')}}">调拨管理</a></li>
          <li><a href="{{url_for('bpTns.view',action='searchpdt')}}">查找商品</a></li>
          <li class="active">申请调拨</li>
        </ol>
    </div>
    <div class="row">
        <div class="btn-toolbar pull-right">
            <div class="btn-group"><button class="btn btn-inverse" onclick="toCancel()">{{BTN_CANCEL}}</button></div>
        </div>
    </div>
                             
    {% for d in data.values() %}
        <div class="row part">
        <h4>商品所在店铺：{{d['shop']}}</h4>
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <td>商品编号</td>
                        <td>商品名称</td>
                        <td>商品描述</td>
                        <td>所在仓位</td>
                        <td>现存数量</td>
                        <td>调拨数量</td>
                    </tr>
                {% for pdt,ivt,ip in d['data'] %}
                     <tr>
                        <td>{{mymacros.render_goto_popup_pdt(pdt, loop.first)}}</td>
                        <td>{{pdt.name}}</td>
                        <td>{{pdt.makeDesc()|f}}</td>
                        <td>{{ivt.fullPath}}</td>
                        <td>{{ip.qty}}</td>
                        <td><input type="text" name="qty_{{pdt.id}}_{{ivt.id}}" value="{{ip.qty}}" class="col-md-1 pdtqty num" maxqty="{{ip.qty}}"/></td>
                     </tr>       
                {% endfor %}
                    </thead>
             </table>
            <div class="btn-toolbar pull-right">
                <div class="btn-group"><button type="button" class="btn btn-primary" onclick="ajaxSnd(this,{{d['shop'].id}})">发送调拨申请</button></div>
            </div>
        </div>
    {% endfor %}

</div>
{% endblock %}